package com.kim.smartring.utils;

import java.util.Calendar;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.TimePickerDialog;
import android.app.TimePickerDialog.OnTimeSetListener;
import android.content.DialogInterface;
import android.content.DialogInterface.OnDismissListener;
import android.view.View;
import android.widget.DatePicker;
import android.widget.DatePicker.OnDateChangedListener;
import android.widget.TimePicker;

import com.kim.smartring.R;

public class DialogUtils {

	/**
	 * 显示设置时间对话框
	 */
	public static void showTimeDialog(final Calendar c, Activity activity,
			OnDismissListener listener) {
		TimePickerDialog dialog = new TimePickerDialog(activity,
				new OnTimeSetListener() {

					@Override
					public void onTimeSet(TimePicker view, int hourOfDay,
							int minute) {
						c.set(Calendar.HOUR_OF_DAY, hourOfDay);
						c.set(Calendar.MINUTE, minute);
					}
				}, c.get(Calendar.HOUR_OF_DAY), c.get(Calendar.MINUTE), true);
		dialog.setOnDismissListener(listener);
		dialog.show();
	}

	/**
	 * 显示日期选择框
	 * 
	 * @param c
	 * @param activity
	 * @param l
	 * @param titleRes
	 * @return
	 */
	public static DatePicker showDateSettingDialog(final Calendar c,
			Activity activity, DialogInterface.OnClickListener l, int titleRes) {

		int y = c.get(Calendar.YEAR);
		int m = c.get(Calendar.MONTH);
		int d = c.get(Calendar.DAY_OF_MONTH);

		View pickerView = activity.getLayoutInflater().inflate(
				R.layout.date_picker, null);
		AlertDialog pickerDialog = new AlertDialog.Builder(activity)
				.setTitle(titleRes).setPositiveButton(R.string.ok, l)
				.setNegativeButton(R.string.cancel, null).setView(pickerView)
				.create();
		pickerDialog.show();

		final DatePicker datePicker = (DatePicker) pickerView
				.findViewById(R.id.date_picker);
		if (PackageUtils.getSDKVersion() > PackageUtils.HONEYCOMB) {
			new Object() {
				public void setCalendarViewShown() {
					datePicker.setCalendarViewShown(false);
				}
			}.setCalendarViewShown();
		}
		datePicker.init(y, m, d, new OnDateChangedListener() {

			@Override
			public void onDateChanged(DatePicker view, int year,
					int monthOfYear, int dayOfMonth) {
				c.set(Calendar.YEAR, year);
				c.set(Calendar.MONTH, monthOfYear);
				c.set(Calendar.DAY_OF_MONTH, dayOfMonth);
			}
		});
		return datePicker;
	}

}
